package com.cirkana;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Location;

import com.cirkana.viewer.location.MockLocation;

public class CirkanaUtil {

	public static SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	public static SimpleDateFormat presentationDateFormat = new SimpleDateFormat(
			"dd/MM/yyyy HH:mm:ss");

	public static Location defaultLocation = new MockLocation(41.38795,
			2.176838); // new MockLocation(41.418625, 2.098653);

	public static String getPresentationDate() {
		Calendar c = Calendar.getInstance();
		return getPresentationDate(c.getTime());
	}

	public static String getPresentationDate(Date date) {
		return presentationDateFormat.format(date);
	}

	public static Bitmap getImageBitmap(String url) throws Exception {
		BufferedInputStream bis = null;
		InputStream is = null;
		try {
			URLConnection conn = new URL(url).openConnection();
			conn.connect();
			is = conn.getInputStream();
			bis = new BufferedInputStream(is);
			return BitmapFactory.decodeStream(bis);
		} catch (Exception e) {
			throw e;
		} finally {
			try {
				bis.close();
				is.close();
			} catch (Exception e) {
				throw e;
			}
		}
	}

	public static void waitSeconds(int n) {
		long t0, t1;
		t0 = System.currentTimeMillis();
		do {
			t1 = System.currentTimeMillis();
		} while (t1 - t0 < 1000);
	}
}
